<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/11/21
 * Time: 10:49 AM
 */

namespace Biz\module\Decoupling\refund;

use Biz\module\Decoupling\AbstractMain;
use Biz\service\Event;
use Doraemon\tools\Log as tLog;
use Doraemon\amqp\Queue as aQueue;
use Biz\model\trade\OrderOperateLog as mOrderOperateLog;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\service\TradeMsg as sTradeMsg;
use Doraemon\tools\ActionLog;
use Biz\model\trade\ShopAddress as mShopAddress;
use Biz\model\trade\Order as mOrder;
use Biz\service\trade\Pay;
use Biz\service\salesRebate\SalesOrder as sSalesOrder;


class AgreeRefund extends AbstractMain
{
    const NAME = '商家同意退款';

    public function main($message, $data)
    {
        //解析 json 并赋值变量
        $appendData = [
            'orderShops' => [],
            'uid' => $data['body']['uid'],
            'addTime' => $data['header']['addtime'],
            'shopId' => $data['body']['shop_id'],
            'orderNo' => $data['body']['order_no'],
            'skuId' => $data['body']['sku_id'],
            'refundOrderNo' => $data['body']['refund_order_no'],
            'needOrderOpLog' => $data['body']['need_order_oplog'],
            'isSys' => $data['body']['is_sys'],
            'addressId' => $data['body']['address_id'],
        ];

        $appendData['orderSku'] = mOrderGoods::getSkuInfo($appendData['orderNo'], $appendData['skuId']);

        $appendData['refundInfo'] = mOrderRefund::getRefundOrderInfo($appendData['refundOrderNo']);

        $appendData['orderInfo'] = mOrder::getOrderInfo($appendData['orderNo']);


        //是否是退货
        $appendData['returnGoods'] = ($appendData['refundInfo']['type_refund'] == mOrderRefund::TYPE_REFUND_ALL);

        $this->appendData($appendData);


    }

    protected function _exec_refund_goods_sendmsg()
    {
        if ($this->data['returnGoods']) {

            if (!in_array($this->data['orderInfo']['from_source'], [mOrder::FROM_SOURCE_PARAGRAPH_MINIPROGRAM])) {

                //给买家下发系统通知
                sTradeMsg::addMsg(301, $this->data['refundInfo']['uid'], $sysMsg = [
                    'refund_order_no' => $this->data['refundOrderNo'],
                    'img' => $this->data['orderSku']['cover_img_thumb_url'],
                ]);

                $this->appendLog('同意退货时给买家下发系统通知成功', array_merge($this->data, $sysMsg));

            }


        }
    }

    protected function _exec_refund_goods_action_log()
    {

        if ($this->data['returnGoods']) {

            //商家同意退货的行为日志
            ActionLog::log($this->data['uid'], ['spu_id' => $this->data['orderSku']['goods_id'], 'order_no' => $this->data['orderNo']], ActionLog::BIZ_AGREE_ORDER_REFUND);

            $this->appendLog('行为日志记录执行完毕', $this->data);
        }
    }

    protected function _exec_refund_amount_sendmsg()
    {
        if (!$this->data['returnGoods']) {

            if (!in_array($this->data['orderInfo']['from_source'], [mOrder::FROM_SOURCE_PARAGRAPH_MINIPROGRAM])) {
                //给买家下发系统通知
                sTradeMsg::addMsg(201, $this->data['refundInfo']['uid'], $sysMsg = [
                    'refund_order_no' => $this->data['refundOrderNo'],
                    'refund_amount' => $this->data['refundInfo']['refund_amount'],
                    'img' => $this->data['orderSku']['cover_img_thumb_url'],
                ]);
                $this->appendLog('给买家下发同意退款的通知成功', array_merge($this->data, $sysMsg));
            }
        }
    }

    protected function _exec_refund_amount_action_log()
    {
        if (!$this->data['returnGoods']) {

            //商家同意退款的行为日志
            ActionLog::log($this->data['uid'], ['spu_id' => $this->data['orderSku']['goods_id'], 'order_no' => $this->data['orderNo']], ActionLog::BIZ_AGREE_ORDER_REFUND_AMOUNT);

            $this->appendLog('行为日志记录执行完毕', $this->data);

        }
    }

    protected function _exec_for_salesrebate()
    {
        try {
            sSalesOrder::afterCloseOrder($this->data['orderNo'], 1, $this->data['orderSku']['goods_id'], $this->data['skuId'],$this->data['refundInfo']['refund_rmb']);

            $this->appendLog('退款成功时回调设计师返利计划', $this->data);

        } catch (\Exception $e) {
            $this->appendLog('退款成功时回调设计师返利计划 出现异常', [
                "sSalesOrder::afterCloseOrder" => [
                    $this->data['orderNo'],
                    1,
                    $this->data['orderSku']['goods_id'],
                    $this->data['skuId'],
                    $this->data['refundInfo']['refund_rmb']
                ],
                "e_msg" => $e->getMessage(),
                "e_line" => $e->getLine(),
                "e_file" => $e->getFile()
            ]);
            throw $e;
        }


    }

    protected function _exec_order_operate_log()
    {
        if ($this->data['needOrderOpLog']) {
            $shopAddress = mShopAddress::getById($this->data['addressId']);

            $addTime = date('Y-m-d H:i:s');
            $textContent = $this->data['returnGoods'] ? vsprintf('%s，请买家在7天内退回商品并上传退货快递单号, 退货地址：%s 收件人：%s 联系电话：%s', [
                ($this->data['isSys'] ? '超时自动同意退货' : '同意退货'),
                !empty($shopAddress) ? $shopAddress['address'] : '',
                !empty($shopAddress) ? $shopAddress['accept_name'] : '',
                !empty($shopAddress) ? $shopAddress['mobile'] : '',
            ]) : ($this->data['isSys'] ? '超时自动同意退款' : '同意退款');

            //增加操作日志
            $result = mOrderOperateLog::createLog($this->data['orderSku']['shop_id'], $this->data['orderSku']['order_no'], mOrderOperateLog::TRADE_TYPE_ORDER_REFUND, $opLog = [
                'operate_type' => mOrderOperateLog::OPERATE_TYPE_AGREE_REFUND_APPLY,
                'operate_user_type' => $this->data['isSys'] ? mOrderOperateLog::OPERATE_USERTYPE_SYS : mOrderOperateLog::OPERATE_USERTYPE_MGT,
                'operate_uid' => $this->data['isSys'] ? 0 : $this->data['shopId'],
            ], $this->data['refundOrderNo'], $textContent, '', [], $addTime);

            $this->appendLog('操作日志记录成功', array_merge($this->data, $opLog, ['text_content' => $textContent]));

        }
    }
}